package com.dhcy.mqtt.service.impl;


import com.dhcy.mqtt.dao.DeviceMapper;
import com.dhcy.mqtt.model.Device;
import com.dhcy.mqtt.par.ht.*;
import com.dhcy.mqtt.result.ht.TableResultData;
import com.dhcy.mqtt.util.AppResultData;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcy.mqtt.dao.DeviceDataMapper;
import com.dhcy.mqtt.model.DeviceData;
import com.dhcy.mqtt.service.DeviceDataService;
import java.util.ArrayList;
import com.dhcy.mqtt.util.IsNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import com.dhcy.mqtt.result.ht.DeviceDataResult;

@Service
public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceData> implements DeviceDataService{
@Autowired
    DeviceDataMapper deviceDataMapper;

@Autowired
DeviceMapper deviceMapper;

    /**
     * 设备数据列表
     * @param deviceDataListPar
     * @return
     */
    @Override
    public TableResultData deviceDataList(DeviceDataListPar deviceDataListPar) {
        IPage<DeviceData> page = new Page<>(deviceDataListPar.getPage(), deviceDataListPar.getPageSize());
        QueryWrapper<DeviceData> queryWrapper = new QueryWrapper<>();
        if(!IsNull.isNullOrEmpty(deviceDataListPar.getDeviceId())){
            queryWrapper.eq("deviceId",deviceDataListPar.getDeviceId());
        }
        if(!IsNull.isNullOrEmpty(deviceDataListPar.getData())){
            queryWrapper.like("data",deviceDataListPar.getData());
        }
        page = deviceDataMapper.selectPage(page, queryWrapper);
        List<DeviceDataResult> resultList = new ArrayList<>();
        for (DeviceData deviceData : page.getRecords()) {
            DeviceDataResult deviceDataResult = new DeviceDataResult();
            BeanUtils.copyProperties(deviceData, deviceDataResult);
            Device device = deviceMapper.selectById(deviceData.getDeviceId());
            if(device!=null){
                deviceDataResult.setDeviceName(device.getName());
                deviceDataResult.setDeviceCode(device.getCode());
            }
            resultList.add(deviceDataResult);
        }
        return TableResultData.success(page.getTotal(), resultList);
    }
    /**
     * 新增设备数据
     * @param deviceDataaddPar
     * @return
     */
    @Override
    public AppResultData deviceDataAdd(DeviceDataAddPar deviceDataaddPar) {
        DeviceData deviceData = new DeviceData();
        BeanUtils.copyProperties(deviceDataaddPar, deviceData);
        if (IsNull.isNullOrEmpty(deviceData.getId())) {
            deviceData.setCreateTime(new Date());
            deviceDataMapper.insert(deviceData);
        } else {
            deviceDataMapper.updateById(deviceData);
        }
        return AppResultData.successMessage("成功");
    }
    /**
     * 删除设备数据
     * @param id
     * @return
     */
    @Override
    public AppResultData deviceDataDel(Integer id) {
        int count = deviceDataMapper.deleteById(id);
        if (count > 0)
            return AppResultData.successMessage("成功");
        else {
            return AppResultData.errorMessage("当前记录不存在");
        }
    }}
